package descentFly.away3D4.Background
{
	import away3d.core.base.SubMesh;
	import away3d.entities.Mesh;
	import away3d.primitives.SkyBox;
	import away3d.vf.utils;
	
	import descentFly.away3D4.chips.df_chipDummy;
	import descentFly.away3D4.chips.df_chipModel;
	import descentFly.away3D4.chips.df_chipSkin;
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.df_baseSpaceElement;
	import descentFly.away3D4.df_points;
	import descentFly.controller.df_userController;
	import descentFly.controller.myFlyController;
	import descentFly.df_space;
	import descentFly.df_utils;
	import descentFly.jiglib.utils;
	
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	
	import jiglib.events.JCollisionEvent;
	import jiglib.geometry.JSphere;
	import jiglib.geometry.JTriangleMesh;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.away3d4.Away3D4Mesh;
	
	import space.sp_utils;
	
	public class df_bgSky extends df_baseSpaceElement {
		public static const FIELD_NEAREST	: String = 'nearest';
		public static const DEFAULT_NEARES	: Number = 400;
		
		private var _skyBox			: SkyBox;
		private var _windows		: Array;
		private var _postPos		: Vector3D;
		private var _curPos			: Vector3D;
		private var _crossCount		: int;
		private var _nearest		: Number;	
		
		public function df_bgSky(a_space:df_awaySpace, a_options: Object) {
			a_options = sp_utils.checkParam(a_options, FIELD_NEAREST, DEFAULT_NEARES);
			super(a_space, a_options);
		}
		
		override protected function initialize(): void {
			super.initialize();
			if (options.skyBox) {
				setSkyBox(options.skyBox);
			}
		}
		
		public function get skyBox(): SkyBox {
			return _skyBox;
		}
		
		protected function setSkyBox(a_skyBox: SkyBox): void {
			_skyBox = a_skyBox;
			initWindows();
			//Space.view.view.scene.addChild(_skyBox);
		}
		
		protected function initWindows(): void {
			_windows 	= new Array();
			_crossCount = 0;
			Space.points.forEach(df_points.TYPE_WND, function(window: Object, i: int): void {
				_windows.push(window.mesh);
			});
			
			if (_windows.length > 0) {
				function setListenFrame(): void {
					Space.app.stage.addEventListener(Event.ENTER_FRAME , doFrame);
				}
				if (Space.myChip) setListenFrame();
				else Space.callWaitEvent(df_space.SETMYCHIP, setListenFrame);
			} else visible = true;
		}
		
		protected function doFrame(e: Event): void {
			if (Space.myChipAway) {
				if (Space.myChipAway.body.isActive && _postPos) {
					_curPos = Space.myChipAway.body.currentState.position.clone();
					if (_curPos.subtract(_postPos).length > 0) 
						checkCollision();
				}
				_postPos = Space.myChipAway.body.currentState.position.clone();
			}
		}
		
		protected function checkCollision(): void {
			var index	: int;
			_nearest = Number.MAX_VALUE;
			
			_windows.forEach(function(window: Mesh, i: int, arr: Array): void {
				var len: Number 	= window.position.subtract(_curPos).length;
				if (len < _nearest) {
					index 		= i;
					_nearest 	= len;
				}
			});
			
			if (_nearest < options[FIELD_NEAREST]) {
				var p1: Vector3D = sp_utils.globalToLocal(_windows[index], _curPos);
				var p2: Vector3D = sp_utils.globalToLocal(_windows[index], _postPos);
				var subMesh: SubMesh = _windows[index].subMeshes[0];
				var result: Vector3D = df_utils.hitTestSubMesh(subMesh, p1, p2);
				if (result) {
					_crossCount++;
				} else if (_crossCount % 2 == 0) visible = true;
			} else if (_crossCount % 2 == 0) visible = false;
		}
		
		public function get visible(): Boolean {
			return _skyBox.parent != null;
		}
		
		public function set visible(a_visible: Boolean): void {
			if (a_visible) {
				if (!_skyBox.parent) Space.view.view.scene.addChild(_skyBox);
			} else if (_skyBox.parent) _skyBox.parent.removeChild(_skyBox);
		}
		
		override public function dispose():void {
			Space.app.stage.removeEventListener(Event.ENTER_FRAME , doFrame);
			_skyBox.dispose(true);
			super.dispose();
		}
	}
}